package com.jsz.peini.service.seller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jsz.peini.bean.seller.SellerDiscount;
import com.jsz.peini.bean.seller.SellerDiscountExample;
import com.jsz.peini.common.util.CommonUtils;
import com.jsz.peini.mapper.seller.SellerDiscountMapper;

/**
 * @author Han Guohong
 * @version 2017-3-27 下午1:47:58
 * 类说明
 */

@Service
public class SellerDiscountService {
	private static final Logger logger = LoggerFactory.getLogger(SellerDiscountService.class);
	@Resource
	private SellerDiscountMapper sellerDiscountMapper;
	
	/**
	 * 获取店铺折扣
	 * @author Han Guohong
	 * @param reques
	 * @param sellerInfoId
	 * @return
	 */
	public SellerDiscount getSellerDiscount(HttpServletRequest request,Integer sellerInfoId
			) {
		
		SellerDiscount sellerDiscount = null;
		try {
			int weekNum = CommonUtils.getWeekNum();
			
			SellerDiscountExample example = new SellerDiscountExample();
			example.createCriteria().andSellerInfoIdEqualTo(sellerInfoId);
			example.setOrderByClause("start_num asc,start_time ASC");
			List<SellerDiscount> list = sellerDiscountMapper.selectByExample(example);
			for (SellerDiscount discount : list) {
				if(discount.getStartNum() <= weekNum && discount.getEndNum() >= weekNum){
					sellerDiscount = discount;
				}
			}
			
			if(sellerDiscount == null && list.size() > 0){
				sellerDiscount = list.get(0);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return sellerDiscount;
	}
}
